﻿/**
 * 視圖中介器
 * 
 * @author LuoDong
 * @email luo4521@online.net.tw
 * 2010-2-3 16:10
 */
package com.addcn.texas.view
{	
	import com.addcn.texas.utils.GetPhoto;
	import flash.display.Bitmap;
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.*;
	import flash.system.ApplicationDomain;
	
	import com.addcn.fog.view.*;
	import com.addcn.fog.control.*;
	import com.addcn.common.event.ParamEvent;
	import com.addcn.common.utils.AppLibary;
	import com.addcn.common.utils.RemoveAllChildren;
	
	import com.addcn.texas.Main;
	import com.addcn.texas.control.*
	import com.addcn.texas.view.*;	
	import com.addcn.texas.model.*;	
		
	public class StartupView extends View
	{		
		private var _container : Sprite;		
		private var _components : Object;
		
		public function StartupView(r_target : Object) {			
			_container = new Sprite;
			r_target.addChild(_container);		
		}			

		override public function notificationsList():void 
		{
			notifications = [
							Main.APP_STARTUP,
							UserInfoModel.UPDATE_USER_INFO,
							UserInfoModel.USER_INFO_ERR,
							UserInfoModel.USER_INFO_MODIFY,
							HallModel.HALLS_NAME,
							];
		}	

		override public function onDataChanged(r_notification:String, r_data:Object = null):void 
		{
			switch (r_notification) {
				case Main.APP_STARTUP:  //执行初始化
					initialize();
					break;
				case UserInfoModel.UPDATE_USER_INFO:					
					//sendNotification(PopupView.HIDE_NOTICE);
					//loginSucc(r_data);
					break;
				case UserInfoModel.USER_INFO_MODIFY:
					_setPhoto(r_data);
					break;
				case UserInfoModel.USER_INFO_ERR:	
					sendNotification(PopupView.HIDE_NOTICE);
					loginFail(r_data.code);
					break;
				case HallModel.HALLS_NAME:
					_setHallButton(r_data);
					break;
			}			
		}
		
		private function initialize() : void 
		{
			_components = {};
			//大厅背景
			var _class = AppLibary.getInstance().getClass("HallBackGround");
			if (_class != null) {
				var _mc = new _class;
				_mc.name = "HallBackGround";				
				_mc.x = 0;
				_mc.y  = 0;
				this._container.addChild(_mc);
				_components["HallBackGround"] = _mc;
			}
			return;
			//大厅顶部按钮
			//_class = AppLibary.getInstance().getClass("com.addcn.texas.view.components.TopMenu");	
			//if(_class){
				//_mc = new _class;					
				//_mc.name = "TopMenu";
				//_mc.x = 200 ;
				//_mc.y = 15;
				//this._container.addChild(_mc);	
				//_mc.addEventListener(_class.EVENT, topMenuClickHandler);
				//_components["TopMenu"] = _mc;
			//}			
		
			//左边菜单模块
			//_class = AppLibary.getInstance().getClass("com.addcn.texas.view.components.LeftModuleUi");
			//if (_class != null) {
				//_mc = new _class;
				//_mc.name = "LeftModuleUi";				
				//_mc.x = 0;
				//_mc.y  = 0;
				//this._container.addChild(_mc);	
				//_components["LeftModuleUi"] = _mc;
			//}	
			//登录模块
			//_class = AppLibary.getInstance().getClass("com.addcn.texas.view.components.LoginModule");
			//if (_class != null) {
				//_mc = new _class;
				//_mc.name = "LoadingUi";				
				//_mc.x = 13;
				//_mc.y  = 80;
				//this._container.addChild(_mc);		
				//_components["LoadingUi"] = _mc;
				//_mc.addEventListener(_class.EVENT, loginBtnClickHandler);
			//}	
		}
		
		
		
		
		//初始化大廳按鈕
		private function _setHallButton(r_data : Object) : void
		{				
			//var _topMenu = _components["TopMenu"];
			//if (!_topMenu) return;
			//var i : int = 0;
			//var _arr : Array = [];
			//for each(var _val in r_data) {
				//_arr[i] = _val.name;
				//i++;
			//}
			//_topMenu.setButtonTxt(_arr);
		}
		
		//相應登錄操作
		private function loginBtnClickHandler(evt : ParamEvent) : void
		{			
			if (evt.params.account == "" || evt.params.pwd == "") {
				sendNotification(PopupView.SHOW_ALERT_BOX, { type: 'alert',code:"9999" } );
				return;
			}
			if (!Controller.getInstance().hasCommand(LoginCommand)) {
				Controller.getInstance().addCommand(LoginCommand);
			}
			evt.target.btnDisabled();
			sendNotification(PopupView.SHOW_NOTICE, "1003");
			sendNotification(LoginCommand, evt.params);		
		}
		
		public function loginSucc(r_data : Object) : void 
		{			
			var _mc = _components["LoadingUi"]; 
			if (_mc != null) {
				var _nick : String = String(r_data.name);
				var _gold : String = String(r_data.gold);				
				_mc.showUserInfo(_nick , _gold);				
				var _photo: String = String(r_data.photo);
				_setPhoto(r_data);
				
			}
		}
		
		private function _setPhoto(r_data) : void
		{
			if (r_data["photo"] != null && r_data["photo"] as Bitmap) {
				_components["LoadingUi"].getPhotoObj().addChild(r_data["photo"]);
			}
		}
		
		//private function topMenuClickHandler(evt : ParamEvent) : void
		//{
			//sendNotification(HallView.GET_ROOMS, evt.params );
		//}
		
		private function loginFail(r_code : String) : void
		{	
			var _obj : Object = { };
			_obj['type'] = "alert";
			_obj["code"] = r_code;
			_obj['fun']  = new Array(this.loginFailAlertCall);
			sendNotification(PopupView.SHOW_ALERT_BOX, _obj);
		}
		
		private function loginFailAlertCall() : void
		{
			var _mc = _components["LoadingUi"];  
			if (_mc != null) {
				_mc.btnEnabled();
			}
		}
		
		override public function destroy():void 
		{
			var _class = AppLibary.getInstance().getClass("com.addcn.texas.view.components.LoginModule");
			_components["LoadingUi"] = addEventListener(_class.EVENT, loginBtnClickHandler);
			//_class = AppLibary.getInstance().getClass("com.addcn.texas.view.components.TopMenu");	
			//_components["TopMenu"].addEventListener(_class.EVENT, topMenuClickHandler);
			
			for each(var _val in _components) {
				_val = null;
			}		
			
			RemoveAllChildren.remove(_container as DisplayObject);			
			_container.parent.removeChild(_container);
			_components = null;
			_container = null;
		}
			
	}
	
}
